package com.inspur.servlet.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.inspur.servlet.entity.Files;
import com.inspur.servlet.util.DefaultConnection;

/**
 * @author  zhangteng 
 * @date 创建时间：2017年7月18日 
 */
public class FilesUploadDao {

	/**
	 * 保存
	 * @param files
	 * @return
	 * @throws Exception
	 */
	public int save(Files files) throws Exception
	{
		int result = 0; 
		Connection  conn = DefaultConnection.getConn();
		PreparedStatement ps = null;
		conn.setAutoCommit(false);// 设置事务手动提交

		String sql = "insert into uploaddb(uuid,uFileName,realName,uploadDire,"
				+ "uploadDate,fileSize,description) values (?,?,?,?,?,?,?)";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, files.getUuid());
			ps.setString(2, files.getuFileName());
			ps.setString(3, files.getRealName());
			ps.setString(4, files.getUploadDire());
			ps.setDate(5, new Date(System.currentTimeMillis()));
			ps.setLong(6, files.getFileSize());
			ps.setString(7, files.getDescription());
			result = ps.executeUpdate();
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return result;
	}
	
	/**
	 * 查找
	 * @return
	 * @throws Exception
	 */
	public List<Files> queryFilesList() throws Exception
	{
		List<Files> list = new ArrayList<Files>();
		Connection  conn = DefaultConnection.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from uploaddb";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next())
			{
				Files files = new Files();
				files.setUuid(rs.getString("uuid"));
				files.setuFileName(rs.getString("uFileName"));
				files.setUploadDire(rs.getString("uploadDire"));
				files.setUploadDate(rs.getDate("uploadDate"));
				files.setFileSize(rs.getLong("fileSize"));
				files.setDescription(rs.getString("description"));
				list.add(files);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 删除
	 * @param uuid
	 * @throws Exception
	 */
	public void delete(String uuid) throws Exception
	{
		Connection  conn = DefaultConnection.getConn();
		PreparedStatement ps = null;
		conn.setAutoCommit(false);// 设置事务手动提交

		String sql = "delete from uploaddb where uuid = ? ";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, uuid);
			ps.executeUpdate();
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
